package tasks

import (
	"itodo/core/localwrap"
	"strconv"
	"sync"

	"github.com/RocksonZeta/wrap/rediswrap"
)

var taskCenters sync.Map

func GetOrNewTaskCenter(id int) (*TaskCenter, error) {
	log.Trace().Func("getOrNewTaskCenter").Int("id", id).Send()
	if g, ok := taskCenters.Load(id); ok {
		return g.(*TaskCenter), nil
	}
	options := TaskCenterOptions{
		Id: id,
	}
	options.RedisChannel = "TaskCenter/" + strconv.Itoa(id)
	options.ChannelSize = 1
	redisOptions, err := rediswrap.ParseUrl(localwrap.Config.Redis)
	if err != nil {
		return nil, err
	}
	options.RedisOptions.Redis = *redisOptions
	g, err := NewTaskCenter(options)
	if err != nil {
		return g, err
	}
	taskCenters.Store(id, g)
	return g, nil
}
